package com.ztools.views;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.util.AttributeSet;

import com.blankj.utilcode.util.RegexUtils;


/**
 * Created by zhangxuehui on 2018/3/19.
 */
public class VerifyCodeButtonView extends AppCompatTextView {

    private String phoneNum;
    private OnSendButtonClickListener mListener;

    public void setOnSendButtonClickListener(OnSendButtonClickListener listener) {
        this.mListener = listener;
    }

    public VerifyCodeButtonView(Context context) {
        this(context, null);
    }

    public VerifyCodeButtonView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VerifyCodeButtonView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    protected void init() {
        this.setIncludeFontPadding(false);
        this.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onSendVerifyCode();
            }
        });
    }


    /**
     * 是否可点击
     */
    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }

    /**
     * 是否可点击
     */
    public void setEnableClick(boolean isEnable) {
        if (!isEnable) {
            startCountDownTimeAnim();
        } else {
            this.setText("获取验证码");
            this.setTextColor(Color.parseColor("#FFFF5532"));
            this.setEnabled(true);
            getAnimator().cancel();
        }
        postInvalidate();
    }

    ValueAnimator animator;

    public void startCountDownTimeAnim() {
        getAnimator().start();
    }

    private ValueAnimator getAnimator() {
        if (animator == null) {
            animator = ValueAnimator.ofInt(60, 0);
            animator.addUpdateListener(animation -> {
                int time = (int) animation.getAnimatedValue();
                this.setFocusable(false);
                this.setFocusableInTouchMode(false);
                if (time > 0) {
                    this.setText("重新获取 " + time);
                    this.setTextColor(Color.parseColor("#FFC1C3CB"));
                    this.setEnabled(false);
                } else if (time <= 0) {
                    this.setText("获取验证码");
                    this.setTextColor(Color.parseColor("#FFFF5532"));
                    this.setEnabled(true);
                }
            });
            animator.setDuration(60 * 1000);
        }
        return animator;
    }

    @Override
    protected void onDetachedFromWindow() {
        if (getAnimator() != null) {
            if (getAnimator().isStarted()) {
                getAnimator().cancel();
                animator = null;
            }
        }
        super.onDetachedFromWindow();
    }

    /**
     * 检查登录按钮是否可以点击
     */
    public boolean checkPhoneNunIsEnable(String phoneNo) {
        if (!TextUtils.isEmpty(phoneNo)
                && RegexUtils.isMobileExact(phoneNo)
                && phoneNo.length() == 11) {
            return true;
        }
        return false;
    }


    /**
     * 手机号码监听
     */
    public interface OnSendButtonClickListener {
        /**
         * 发送验证码
         */
        void onSendVerifyCode();

    }

}
